﻿


(function () {
    var module = angular.module('mebService', []);
    module.service('Msg', ['$rootScope', function ($rootScope) {
        var service = {
            title: "标题",
            content: "",
            close: "关闭",
            okText: "确定",
            okBtn: false,
            showMe: false,
            okShow: function (title, content, ok,close) {
                isVidate.state = false;
                isVidate.isgoon = false;
                service.close = "关闭";
                service.okText = "确定";
                if (title != null) {
                    service.title = title;
                }
                if (content != null) {
                    service.content = content;
                }
                if (ok != null) {
                    service.callBack = ok;
                }
                if(close!=null){
                    service.closeDetail = close;
                }
                service.okBtn = true;
                service.showMe = !service.showMe;
            },
            ok: function () {
                if (isVidate.state&&isVidate.isgoon) {
                    service.callBack();
                    service.showMe = !service.showMe;
                } else if (!isVidate.state) {
                    service.callBack();
                    service.showMe = !service.showMe;
                }
            },
            callBack: function () {},
            show: function (title, content,close) {
                if (title != null) {
                    service.title = title;
                }
                if (content != null) {
                    service.content = content;
                }
                if(close!=null){
                    service.closeDetail = close
                }
                service.okBtn = false;
                service.showMe = !service.showMe;
            },
            closeBtn: function () {
                service.closeDetail();
                service.showMe = !service.showMe;
            },
            closeDetail: function () { },
            remove: function (title, content, ok) {
                service.close = "否";
                service.okText = "是";
                if (title != null) {
                    service.title = title;
                }
                if (content != null) {
                    service.content = content;
                }
                if (ok != null) {
                    service.callBack = ok;
                }
                service.okBtn = true;
                service.showMe = !service.showMe;
            }
        }
        return service;
    }]).service('paging', ['$rootScope', function ($rootScope) {
        var service = {
            list: [],
            next: 0,
            prev: 0,
            paging: true,
            pageIndex: 0,
            pageCountent: 0,
            size: 0,
            recordCount: 0,
            init: function (PageIndex, PageCount, Size, RecordCount, getDate) {
                service.pageIndex = PageIndex;
                service.pageCount = PageCount;
                service.size = Size;
                Size = 10;
                service.recordCount = RecordCount;
                PageIndex > 0 ? PageIndex-- : null;
                service.paging = true;
                if (PageCount == 0) {
                    service.paging = false;
                } else if (PageCount == 1) {
                    service.list = [1];
                } else if (PageCount > 1) {
                    if (PageCount <= Size) {
                        service.list = [];
                        for (var i = 0; i < PageCount; i++) {
                            service.list.push(i + 1);
                        }
                    } else {
                        if (service.list != null) {
                            var m = 0;
                            for (var i = 0; i < service.list.length; i++) {
                                PageIndex != service.list[i] ? m++ : null;
                            }
                            if (m == service.list.length) {
                                updatePageNum();
                            } else {
                                if (PageIndex == service.list[service.list.length - 2] || PageIndex == service.list[0] - 1) {
                                    updatePageNum();
                                }
                            }
                        } else {
                            for (var i = 0; i < Size; i++) {
                                service.list.push(i);
                            }
                        }
                    }
                }
                updateNextPrev();
                //更新上一页下一页
                function updateNextPrev() {
                    PageIndex < service.list[0] ? service.prev = service.list[0] : service.prev = PageIndex;
                    PageIndex + 2 > service.list[service.list.length - 1] ? service.next = service.list[service.list.length - 1] : service.next = PageIndex + 2;
                }
                //更新页码
                function updatePageNum() {
                    var count = PageIndex + parseInt(Size / 2);
                    var first = PageIndex - parseInt(Size / 2);
                    count > PageCount ? count = PageCount : null;
                    first < 0 ? first = 0 : null;
                    first + Size > PageCount ? null : (first + Size == count ? null : count = first + Size);
                    count - Size > 0 ? (count - Size == first ? null : first = count - Size) : null;
                    PageIndex - 1 < first ? service.prev = first : service.prev = PageIndex - 1;
                    PageIndex + 1 > count ? service.next = count : service.next = PageIndex + 1;
                    service.list = [];
                    for (first; first < count; first++) {
                        service.list.push(first + 1);
                    }
                }
                service.filter = getDate;
            },
            getData: function (index) {
                if (index != service.pageIndex) {
                    service.filter(index);
                }
            },
            filter: function () {

            }
        };
        return service;
    }]).service('Verify', function ($rootScope) {
        var service = {
            init: function (data_form) {  //新增时初始化
                $('*[data-form="' + data_form + '"]').find('*[verify]').each(function () {
                    if (typeof ($(this).attr('empty')) != "undefined") {
                        $(this).attr('verify',false);
                    } else {
                        $(this).attr('verify', true);
                    }
                });
            },
            initEdit: function (data_form) {  //修改时初始化
                $('*[data-form="' + data_form + '"]').find('*[verify]').each(function () {
                    $(this).attr('verify', true);
                });
            },
            isPass: function (data_form) {
                var t = true;
                //遍历页面上调用验证的标签
                $('*[data-form="' + data_form + '"]').find('*[verify]').each(function () {
                    var parOjbs = $(this).parents();
                    var isPass = true;
                    for (var i = 0; i < parOjbs.length; i++) {
                        //隐藏的控件不验证
                        if ($(parOjbs[i]).css('display') == 'none') {
                            isPass = false;
                            //console.log("我不需要验证！")
                            break;
                        }
                    }
                    if (isPass&&$(this).attr('verify') == 'false') {
                        //console.log($(this).attr("placeholder"));
                        t = false;
                        //查找没有通过验证的提示并显示
                        $('*[data-' + $($(this).parents('*[data-form]')[0]).attr('data-form') + '-' + $(this).attr('name') + ']').attr('data-show', true);
                    } else if (isPass&&$(this).attr('empty') != undefined && $(this).val().replace(/[ ]/g, "") == '') {
                        t = false;
                        //查找没有通过验证的提示并显示
                        $('*[data-' + $($(this).parents('*[data-form]')[0]).attr('data-form') + '-' + $(this).attr('name') + ']').attr('data-show', true);
                    }
                });
                return t;
            }
        }
        //var service = function (data_form) {
        //    var t = true;
        //    //遍历页面上调用验证的标签
        //    $('*[data-form="' + data_form + '"]').find('*[verify]').each(function () {
        //        var parOjbs = $(this).parents();
        //        var isPass = true;
        //        for(var i=0;i<parOjbs.length;i++)
        //        {
        //            //隐藏的控件不验证
        //            if ($(parOjbs[i]).css('display') == 'none') {
        //                isPass = false;
        //                //console.log("我不需要验证！")
        //                break;
        //            }
        //        }
        //        if ($(this).attr('verify') == 'false' && isPass) {
        //            console.log($(this).attr("placeholder"));
        //            t = false;
        //            //查找没有通过验证的提示并显示
        //            $('*[data-' + $($(this).parents('*[data-form]')[0]).attr('data-form') + '-' + $(this).attr('name') + ']').attr('data-show',true);
        //        }
        //    });
        //    return t;
        //};
        return service;
    });
}).call(this);